# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.volume_spark_sql_operator import VolumeRichSqlSensor
from spmi_volume.dws.dws_volume_cost_kilogram_sum_entry_mark_m import dws_volume_cost_kilogram_sum_entry_mark_m
from spmi_volume.dws.dws_volume_cost_kilogram_sum_m import dws_volume_cost_kilogram_sum_m
from spmi_volume.dws.dws_volume_cost_customer_sum_m import dws_volume_cost_customer_sum_m
from spmi_volume.dws.dws_volume_cost_customer_sum_entry_mark_m import dws_volume_cost_customer_sum_entry_mark_m


#Hive表名
hive_table = "dm_5_volume"
#任务名
task_id = f"dm_5_volume{hive_table}"

dm_5_volume = VolumeRichSqlSensor(
    task_id=f'{task_id}',
    task_concurrency=1,
    pool_slots=2,
    master='yarn',
    name=f'{task_id}_{{{{ execution_date | date_add(1) | cst_ds }}}}',
    sql=f'spmi_volume/dm/{hive_table}/execute.sql',
    retries=0,
    pool='spmi_piece',
    email=['yushuo@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    execution_timeout=timedelta(hours=4),
    yarn_queue='route',

    driver_memory='4G',
    driver_cores=2,
    executor_cores=6,
    executor_memory='24G',
    num_executors=60,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 80,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 120,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 1440,
          'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=6 -XX:ConcGCThreads=2'
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 1000,
              'hive.exec.max.dynamic.partitions.pernode': 1000,
              },
)
dm_5_volume << [dws_volume_cost_kilogram_sum_entry_mark_m,
dws_volume_cost_kilogram_sum_m,
dws_volume_cost_customer_sum_m,
dws_volume_cost_customer_sum_entry_mark_m
]